import { CameraControl } from "./cameraControl";
import { KeyboardKeyHold } from "hold-event";
import { sceneOption as opts } from "../state";

export class KeyboardControl {
  private model: THREE.Object3D;
  constructor(model: THREE.Object3D) {
    this.model = model;
    this.init();
  }

  private init(): void {
    let that = this;
    const KEYCODE = {
      W: 87,
      A: 65,
      S: 83,
      D: 68,
      ARROW_LEFT : 37,
      ARROW_UP   : 38,
      ARROW_RIGHT: 39,
      ARROW_DOWN : 40,
    };
    const wKey = new KeyboardKeyHold(KEYCODE.W, 100);
    const aKey = new KeyboardKeyHold(KEYCODE.A, 100);
    const sKey = new KeyboardKeyHold(KEYCODE.S, 100);
    const dKey = new KeyboardKeyHold(KEYCODE.D, 100);
    
    aKey.addEventListener("holding", function (event) {
      opts.cameraControl.truck(-0.01 * event.deltaTime, 0, true);
    });
    dKey.addEventListener("holding", function (event) {
      opts.cameraControl.truck(0.01 * event.deltaTime, 0, true);
    });
    wKey.addEventListener("holding", function (event) {
      opts.cameraControl.forward(0.01 * event.deltaTime, true);
      let position = opts.camera.position.clone();
      // that.model.position.set(position.x, position.y, position.z);
      that.model.setRotationFromMatrix(matrix);
    });
    sKey.addEventListener("holding", function (event) {
      opts.cameraControl.forward(-0.01 * event.deltaTime, true);
    });
  }
}
